package com.dhecp.project.api.util;

import com.dhecp.common.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ApiCheckUtils
 * @Description TODO
 * @Author alula
 * @Date 2022-08-25 17:10
 */
public class ApiCheckUtils {

    /**
     * /验证公共参数的合法性
     * @param appId
     * @param sign
     * @param timestamp
     * @param body
     * @return
     */
    public static String checkPublicParamsValid(String appId, String sign,long timestamp, String body, boolean isSign){
        String result = "";
        if(StringUtils.isEmpty(appId)){
            result = "appId参数不能为空！";
            return  result;
        }
        if(StringUtils.isEmpty(sign)){
            result = "sign参数不能为空！";
            return  result;
        }
        if(timestamp <= 0){
            result = "timestamp参数不能为空或者为零！";
            return  result;
        }
        if(StringUtils.isEmpty(body)){
            result = "body参数不能为空！";
            return  result;
        }
        if (!appId.equals(ApiConfig.getDasAppKey())){
            result = "appId参数为非法ID！";
            return  result;
        }
        Map<String, String> params = new HashMap<>();
        params.put("appId", appId);
        params.put("body", body);
        params.put("timestamp", String.valueOf(timestamp));
        String appendStr = ApiMD5Utils.getAppendString(params);
        String newSign = ApiMD5Utils.MD5Encode((appendStr + ApiConfig.getDasAppSecret()));
        if (sign.contains(newSign) == false && isSign == true) {
            result = "签名认证失败！正确为：" + newSign;
            return  result;
        }
        return  result;
    }
}
